﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TISSEAN
{
    [DialogAttribute(Prefix = "ETSMI_")]
    public partial class MutualDialog : Form
    {
        const long partitions = 16;
        const long corrLength = 20;
        const long length = long.MaxValue;
        const long exclude = 0;
        const long column = 1;

        public string FileName
        {
            get
            {
                return tbFileName.Text;
            }
            set
            {
                tbFileName.Text = value;
            }
        }

        public string Separator
        {
            get
            {
                return cbSeparator.SelectedItem.ToString();
            }
            set
            {
                cbSeparator.SelectedItem = value;
            }
        }

        public long Length
        {
            get
            {
                long res = 0;

                if (string.IsNullOrEmpty(tbPoints.Text))
                {
                    return length;
                }
                else
                {
                    if (long.TryParse(tbPoints.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Number of points must long type");
                    }
                }
            }
            set
            {
                tbPoints.Text = value.ToString();
            }
        }

        public long Exclude
        {
            get
            {
                long res = 0;

                if (string.IsNullOrEmpty(tbExclude.Text))
                {
                    return exclude;
                }
                else
                {
                    if (long.TryParse(tbExclude.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Number of lines to be ignored must long type");
                    }
                }
            }
            set
            {
                tbExclude.Text = value.ToString();
            }
        }

        public long Column
        {
            get
            {
                long res = 0;

                if (string.IsNullOrEmpty(tbColumn.Text))
                {
                    return column;
                }
                else
                {
                    if (long.TryParse(tbColumn.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Column number must long type");
                    }
                }
            }
            set
            {
                tbColumn.Text = value.ToString();
            }
        }

        public long Boxes
        {
            get
            {
                long res = 0;

                if (string.IsNullOrEmpty(tbBoxes.Text))
                {
                    return partitions;
                }
                else
                {
                    if (long.TryParse(tbBoxes.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Number of boxes must long type");
                    }
                }
            }
            set
            {
                tbBoxes.Text = value.ToString();
            }
        }

        public long Delay
        {
            get
            {
                long res = 0;

                if (string.IsNullOrEmpty(tbDelay.Text))
                {
                    return corrLength;
                }
                else
                {
                    if (long.TryParse(tbDelay.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Number of boxes must long type");
                    }
                }
            }
            set
            {
                tbDelay.Text = value.ToString();
            }
        }


        public MutualDialog()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbFileName.Text) && cbSeparator.SelectedItem != null)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;

                this.Close();
            }
            else
            {
                MessageBox.Show("Data file name or separator was not set");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;

            this.Close();
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = false;
            dlg.ShowDialog();

            if (!string.IsNullOrEmpty(dlg.FileName))
            {
                tbFileName.Text = dlg.FileName;
            }

        }
    }
}
